package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional
@Slf4j
public class SetmealServiceImpl implements SetmealService {


    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;


    // 套餐列表 分页
    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        //构建条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        //判断name不为null成立true      s * f S where name(列) like %name%
        qw.like(StrUtil.isNotBlank(name), Setmeal::getName, name);
        //开启分页
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        //分页查询
        page = setmealMapper.selectPage(page, qw);
        // 遍历套餐列表
        for (Setmeal setmeal : page.getRecords()) {
            // 根据category_id 查询并封装分类名称
            Category category = categoryMapper.selectById(setmeal.getCategoryId());
            setmeal.setCategoryName(category.getName());
            // 根据id查询并封装菜品列表
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<SetmealDish>();
            wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
            List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
            setmeal.setSetmealDishes(setmealDishList);
        }
        // 返回组装后的数据
        return page;

    }


    // 新增套餐
    @Override
    public void save(Setmeal setmeal) {
        setmealMapper.insert(setmeal);
        log.info("套餐主键返回：" + setmeal.getImage());
        // 遍历前端提交套餐菜品列表
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishList) {
            // 关联套餐id
            setmealDish.setSetmealId(setmeal.getId());
            // 保存中间表
            setmealDishMapper.insert(setmealDish);
        }
    }


    // 删除套餐
    @Override
    public void deleteBatch(List<Long> ids) {
        // 先查询套餐状态，是否有启售 select count(*) from setmeal where status=1 and id in(1,3,5);
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId, ids); // id in(1,3,5);
        qw.eq(Setmeal::getStatus, 1); //   status=1
        Integer count = setmealMapper.selectCount(qw);
        if (count > 0) {
            throw new CustomException("套餐中有启售状态，请先停售再删除~~~");
        }

        // 删套餐
        setmealMapper.deleteBatchIds(ids);

        // 删除套餐内菜品中间表
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishMapper.delete(wrapper);
    }


    //套餐列表展示
    @Cacheable(value = "Setmeal",key = "#categoryId")
    @Override
    public List<Setmeal> findList(Long categoryId, Integer status) {
        log.info("---------存入redis----------");
        //构建条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId, categoryId);
        qw.eq(Setmeal::getStatus, status);
        //查询
        List<Setmeal> setmealList = setmealMapper.selectList(qw);
        return setmealList;
    }


    //点击套餐 显示套餐信息
    @Override
    public List<Dish> findBySetmealDish(Long id) {
        //根据id查询套餐菜品关系表(SetmealDish)
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(qw);
        //以上代码不可以没有图片信息(前台不显示图片)

        //创建一个集合用于封装返回菜品信息
        List<Dish> dishList = new ArrayList<>();

        //遍历拿到每个菜品关系表SetmealDish
        for (SetmealDish setmealDish : setmealDishList) {
            //通过(SetmealDish)表 中的菜品外键 dishId(同时也是dish表的主键id) 直接查找图片信息image
            LambdaQueryWrapper<Dish> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(Dish::getId, setmealDish.getDishId());
            //在循环里一次拿到一个菜品设置就可以
            Dish dish = dishMapper.selectOne(qw2);
          /*  //第二种方法 设置 实体属性 并且方法返回值public List<SetmealDish>
           //取出菜品图片设置到setmealDish表里
            setmealDish.setImage(dish.getImage());
            setmealDish.setDescription(dish.getDescription());*/

            dish.setCopies(setmealDish.getCopies());//设置份数
            dishList.add(dish);//添加到返回集合

        }

        //返回
        return dishList;
      //  return setmealDishList;
    }
}
